﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace HuaProject.Web.Admin
{
    public partial class ProductAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlType.DataSource = new DAL.ProductType().GetList("");
                ddlType.DataTextField = "name";
                ddlType.DataValueField = "id";
                ddlType.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Model.Product pmo = new Model.Product();
            pmo.Title = DAL.Tool.UrnHtml(txtTitle.Text.Trim());
            string newFile = Server.MapPath("~/Images/product/" + DateTime.Now.ToString("yyyy") + "/");
            if (!Directory.Exists(newFile)) //如果文件夹不存在则创建
            {
                Directory.CreateDirectory(newFile);
            }
            newFile = newFile + DateTime.Now.ToString("MM") + "/";
            if (!Directory.Exists(newFile)) //如果文件夹不存在则创建
            {
                Directory.CreateDirectory(newFile);
            }
            newFile = newFile + DateTime.Now.ToString("dd") + "/";
            if (!Directory.Exists(newFile)) //如果文件夹不存在则创建
            {
                Directory.CreateDirectory(newFile);
            }
            FileUpload1.SaveAs(newFile + FileUpload1.FileName);
            pmo.Piicture = "Images/product/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/" + FileUpload1.FileName;
            //pmo.Remark = DAL.Tool.UrnHtml(txtTitle.Text.Trim());
            pmo.Type = ddlType.SelectedItem.Text;
            pmo.Coding = DAL.Tool.UrnHtml(txtCoding.Text.Trim());
            pmo.HoleSize = DAL.Tool.UrnHtml(txtHoleSize.Text.Trim());
            pmo.Power = DAL.Tool.UrnHtml(txtPower.Text.Trim());
            pmo.Voltage = DAL.Tool.UrnHtml(txtVoltage.Text.Trim());
            pmo.Rating = DAL.Tool.UrnHtml(txtRating.Text.Trim());
            pmo.Temperature = DAL.Tool.UrnHtml(txtTemperature.Text.Trim());
            pmo.Material = DAL.Tool.UrnHtml(txtMaterial.Text.Trim());

            foreach (ListItem li in cblColors.Items)
            {
                if (li.Selected)
                {
                    pmo.Colors += li.Value + ";";
                }
            }
            pmo.Colors = pmo.Colors.TrimEnd(';');
            // pmo.Explain = DAL.Tool.UrnHtml(txtExplain.Text.Trim());
            pmo.UpdateDate = pmo.InsertDate = DateTime.Now;
            pmo.InsertUid = DAL.Users.GetNowLoginId();
            new DAL.Product().Add(pmo);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('添加成功。');", true); return;
        }
    }
}